package com.easygem.controller.business;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.easygem.business.service.BgClientFollowService;
import com.easygem.business.service.ClientConcernService;
import com.easygem.common.model.business.BgClient;
import com.easygem.common.model.business.BgClientFollow;
import com.easygem.common.model.system.Employee;
import com.easygem.common.model.system.User;
import com.easygem.common.response.ActiveUser;
import com.easygem.common.response.ResponseBean;
import com.easygem.common.response.ResponseMessage;
import com.easygem.common.vo.business.BgClientVO;
import com.easygem.common.vo.system.PageVO;
import com.easygem.system.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 廖思炜
 * @version 1.0
 * @className ClientConcernController
 * @Description TODO 客户关怀表示层控制器
 * @date 2025/1/14 10:57
 */
@Api(tags = "客户关怀管理")
@RestController
@RequestMapping("/clientConcern")
public class ClientConcernController {

    @Autowired
    private ClientConcernService clientConcernService;

    @Autowired
    private BgClientFollowService bgClientFollowService;
    @Autowired
    private EmployeeService employeeService;

    /**
     * 得到客户关怀页面数据
     * @param pageNum
     * @param pageSize
     * @param bgClientConcernVO
     * @return
     */
    @GetMapping("/getClientConcerns")
    public ResponseBean getClientConcerns(
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            BgClientVO bgClientConcernVO
    ) {
        PageVO<BgClient> pageVO=clientConcernService.getClientConcerns(bgClientConcernVO,pageNum,pageSize);
        return ResponseBean.success(pageVO);
    }

    /**
     * 得到所有员工
     * @return
     */
    @GetMapping("/getEmployees")
    public ResponseBean getEmployees(){
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        Employee employee = activeUser.getEmployee();
        if (employee.getType()!=0){
            return  ResponseBean.success();
        }
        return ResponseBean.success(employeeService.findAll());
    }
    /**
     * 得到所有客户类型
     * @return
     */
    @GetMapping("/getAllClientTypes")
    public ResponseBean getAllClientTypes(){
        return ResponseBean.success(clientConcernService.getAllClientTypes());
    }

    /**
     * 操作关注状态
     * @param bgClientConcernVO
     * @return
     */
    @PutMapping("/updateAttention")
    @RequiresPermissions({"bg_client:attention"})
    public ResponseBean updateAttention(@RequestBody BgClientVO bgClientConcernVO){
        try {
            clientConcernService.updateAttention(bgClientConcernVO);
            return ResponseBean.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("操作失败");
        }
    }

    /**
     * 续签
     * @param bgClientConcernVO
     * @return
     */
    @PutMapping("/renew")
    @RequiresPermissions({"bg_client:renew"})
    public ResponseBean renew(@RequestBody BgClientVO bgClientConcernVO){
        try {
            clientConcernService.renew(bgClientConcernVO);
            return ResponseBean.success("续签成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("续签失败");
        }
    }

    /**
     * 添加跟进
     * @param bgClientFollow
     * @return
     */
    @PostMapping("/addBgClientFollow")
    @RequiresPermissions({"bg_client_follow:edit"})
    public ResponseBean addBgClientFollow(@RequestBody BgClientFollow bgClientFollow){
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        bgClientFollow.setEmpId(activeUser.getEmployee().getEmpId());
        bgClientFollow.setFollowTime(Timestamp.valueOf(LocalDateTime.now()));
        bgClientFollowService.addBgClientFollow(bgClientFollow);
        return ResponseBean.success("跟进成功");
    }
    /**
     * 客户跟进详情
     */
    @ApiOperation(value = "跟进详情", notes = "根据客户编号获取跟进详情")
    @GetMapping("/getBgClientFollowListByClientId")
    @ResponseBody
    public ResponseMessage<List<BgClientFollow>> getBgClientFollowListByClientId(@RequestParam(value = "clientId") Long clientId) {
        List<BgClientFollow> BgClientFollowList = bgClientFollowService.getBgClientFollowListByClientId(clientId);
        return ResponseMessage.success(BgClientFollowList);
    }

    @ApiOperation("得到appId")
    @PostMapping("/getAppId")
    public ResponseBean getAppId(@RequestBody Map<String, Object> params) {
        return ResponseBean.success(clientConcernService.getAppIdByName(
                (String) params.get("accountName")
        ));
    }

    @ApiOperation("得到签名-余额特供版")
    @PostMapping("/getSign")
    public ResponseBean getSign(@RequestBody Map<String, Object> params){
        Map<String,Object> map=new HashMap<>();
        System.out.println("::::::::"+params);
        map.put("appId",params.get("appId"));
        String sign = sign(map,clientConcernService.getAppSecretByName(params.get("accountName").toString()));
        return ResponseBean.success(sign);
    }

    public String sign(Map<String, Object> map, String appSecret) {
        StringBuilder sb = new StringBuilder(appSecret);
        for (Map.Entry entry : MapUtil.sort(map).entrySet()) {
            if (ObjectUtil.isNotEmpty(entry.getValue())) {
                sb.append(entry.getKey()).append(entry.getValue());
            }
        }
        return SecureUtil.md5(sb.toString()).toUpperCase();
    }
}
